package com.atl.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.atl.enity.po.AgentUser;
import com.atl.enity.web.dto.AdminUserDTO;
import com.atl.enity.web.dto.AgentUserAddDTO;
import com.atl.enity.web.dto.AgentUserSearchDTO;
import com.atl.execption.BusinessException;
import com.atl.mapper.AgentUserMapper;
import com.atl.service.IAgentUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.BeanProperty;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 代理商用户表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@Service
public class AgentUserServiceImpl extends ServiceImpl<AgentUserMapper, AgentUser> implements IAgentUserService {


    @Resource
    private AgentUserMapper agentUserMapper;

    /**
     *
     * 分页查询
     *
     * @param agentUserDTO
     * @return
     */
    @Override
    public Page<AgentUser> searchUser( AgentUserSearchDTO agentUserDTO) {
        QueryWrapper<AgentUser> queryWrapper = new QueryWrapper<>();

        String nickname = agentUserDTO.getUsername();

        //LocalDateTime dateTime = date.atTime(LocalTime.MIN);

        Integer isDisable = agentUserDTO.getIsDisable();
        Integer inspectStatus = agentUserDTO.getInspectStatus();
        String operateType = agentUserDTO.getOperateType();
        String shareType = agentUserDTO.getShareType();

        String accountType = agentUserDTO.getAccountType();

        if (StringUtils.isNotBlank(nickname)) {
            queryWrapper.like("username", nickname);
        }

        String startTime = agentUserDTO.getStartTime();
        String endTime = agentUserDTO.getEndTime();
        if (startTime != null && endTime != null) {
            LocalDateTime date1 = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atTime(LocalTime.MIN);
            LocalDateTime date2 = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atTime(LocalTime.MIN);
            queryWrapper.between("create_time",date1, date2);
        }

        if (StringUtils.isNotBlank(accountType)) {
            queryWrapper.eq("account_type", accountType);
        }

        if (isDisable != null) {
            queryWrapper.eq("is_disable", isDisable);
        }
        if (inspectStatus != null) {
            queryWrapper.eq("inspect_status", inspectStatus);
        }
        if (StringUtils.isNotBlank(operateType)) {
            queryWrapper.eq("operate_type", operateType);
        }
        if (StringUtils.isNotBlank(shareType)) {
            queryWrapper.eq("share_type", shareType);
        }

        return this.page(new Page<>(agentUserDTO.getCurrent(), agentUserDTO.getPageSize()),
                queryWrapper);

        //return queryWrapper.page(new Page<>(agentUserDTO.getCurrent(), agentUserDTO.getPageSize()));
    }


    /**
     *
     * 添加
     *
     * @param agentUserAddDTOUser
     * @param request
     * @return
     */
    @Override
    public boolean addAgetnUser(AgentUserAddDTO agentUserAddDTOUser, HttpServletRequest request) {


        AgentUser one = this.lambdaQuery()
                .eq(AgentUser::getAccount, agentUserAddDTOUser.getAccount())
                .one();

        if (one != null) {
            throw new BusinessException(-1, "用户名已存在");
        }


        AgentUser agentUser = new AgentUser();
        BeanUtils.copyProperties(agentUserAddDTOUser, agentUser);

        //@ApiModelProperty(value = "其他状态")
        //private Integer status;




        //@ApiModelProperty(value = "父级管理员id")
        //private Long adminParentId;
        //
        //@ApiModelProperty(value = "父级分销代理商id")
        //private Long agentParentId;

        //@ApiModelProperty(value = "推荐邀请关系树")
        //private String invitationPath;


        //复制
        agentUser.setCreateTime(LocalDateTime.now());
        agentUser.setLastLoginTime(LocalDateTime.now());
        agentUser.setTimestamp(LocalDateTime.now());
        agentUser.setIsDeleted(0);
        agentUser.setUserInviteCode("lkajflja298034ujmf8u4re9jw9ijsdf98u239js9jf2938rfjwef");
        agentUser.setInspectStatus(1);
        agentUser.setNickname(agentUserAddDTOUser.getUsername());
        agentUser.setPassword(DigestUtil.md5Hex(agentUser.getPassword()));


        boolean save = save(agentUser);
        return save;
    }



    /**
     *
     * 修改用户信息
     *
     * @param agentUser
     * @param request
     * @return
     */
    @Override
    public boolean updateAgetnUser(AgentUser agentUser, HttpServletRequest request) {


        if (agentUser.getPassword() != null) {
            agentUser.setPassword(DigestUtil.md5Hex(agentUser.getPassword()));
        }

        boolean update = updateById(agentUser);
        return update;
    }


    /**
     *
     * 登录
     *
     * @param userAccount
     * @param userPassword
     * @param request
     * @return
     */
    @Override
    public AgentUser login(String userAccount, String userPassword, HttpServletRequest request) {
        AgentUser one = this.lambdaQuery()
                .eq(AgentUser::getAccount, userAccount)
                .eq(AgentUser::getPassword, DigestUtil.md5Hex(userPassword))
                .one();


        if (one == null) {
            throw new BusinessException(-1, "用户名或密码错误");
        }

        return null;
    }
}
